﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BlackBird.Models;
using BlackBird.DataAccess;
using System.Web.Mvc;

namespace BlackBird.ControllerHelpers
{
	public class QuanXianAndProcessHelper
	{
		/// <summary>
		/// 获得比价单 的所有操作StepNumber
		/// </summary>
		/// <param name="supportedType"></param>
		/// <returns></returns>
		public static int?[] GetAllStepNumber(SupportTypes supportedType)
		{
			int processTypeId = GlobalObjectBuffer.AllProcessTypes.FirstOrDefault(p => p.Name == supportedType.ToString()).idProcessType;

			var processStepsByTypeId = GlobalObjectBuffer.AllProcess.Where(p => p.ProcessType_idProcessType == processTypeId).Select(p => p.StepNumber);

			var stepsList = processStepsByTypeId.ToList();

			stepsList.Sort();

			return stepsList.ToArray();
		}

        /// <summary>
        /// 检查传入的stepNumber是否有效, 比如是否在有效范围内
        /// </summary>
        /// <param name="supportedType"></param>
        /// <param name="stepNumber"></param>
        /// <returns></returns>
        public static bool IsStepNumberAvailable(SupportTypes supportedType, int stepNumber)
        {
            var stepNumberList = GetAllStepNumber(supportedType);
            if (stepNumberList == null) return false;

            var findStepNumberObject = stepNumberList.FirstOrDefault(p => p == stepNumber);
            return findStepNumberObject != null;
        }

		/// <summary>
        /// 获得User在 Process表中的 StepNumber 
		/// </summary>
		/// <param name="supportedType"></param>
		/// <param name="currentUser"></param>
		/// <returns></returns>
		public static int?[] GetUserStepNumber(SupportTypes supportedType, user currentUser)
		{

			int processTypeId = GlobalObjectBuffer.AllProcessTypes.FirstOrDefault(p => p.Name == supportedType.ToString()).idProcessType;

			var usergroups = GlobalObjectBuffer.GetGroupByUserId(currentUser.idUser);
			var groupIds = usergroups.Select(p => p.idGroup);

			var userStepNumbers = GlobalObjectBuffer.AllProcess.Where(p => p.ProcessType_idProcessType == processTypeId &&groupIds.Contains(p.Group_idGroup)).Select(p => p.StepNumber);

			return userStepNumbers.ToArray();
		}

        /// <summary>
        /// Check if current user can creat Bijiadan or not
        /// </summary>
        /// <param name="ctrl"></param>
        /// <returns></returns>
        public static bool IsEnableToCreateBijiadan(Controller ctrl)
        {
            //bijiadan is the first step (0 based)
            int firstStep = 0;
            return IsEnableToOperationBiJiaDan(ctrl, firstStep);
        }

        public static bool IsEnableToOperationBiJiaDan(Controller ctr, int currentStepNumber)
        {
            var currentUser = AccountHelper.GetCurrentUser(ctr);
            return QuanXianAndProcessHelper.IsEnableToOperation(SupportTypes.BiJiaDan, currentUser, currentStepNumber);
        }

        /// <summary>
        /// 判断用户有没有权限操作
        /// </summary>
        /// <param name="supportedType"></param>
        /// <param name="currentUser"></param>
        /// <param name="stepNumber"></param>
        /// <returns></returns>
		public static bool IsEnableToOperation(SupportTypes supportedType, user currentUser, int stepNumber)
		{
            int?[] userAllProcessNumber = GetUserStepNumber(supportedType, currentUser);

			if (userAllProcessNumber == null)
				return false;

            int? x = userAllProcessNumber.FirstOrDefault(f => f == stepNumber);

			return x != null;
		}

        public static bool IsEnableToCreateFuKuandan(Controller ctrl)
        {
            //fukuandan is the first step (0 based)
            int firstStep = 0;
            var currentUser = AccountHelper.GetCurrentUser(ctrl);
            return QuanXianAndProcessHelper.IsEnableToOperation(SupportTypes.FukuanDan, currentUser, firstStep);
        }

		/// <summary>
		/// 一个用户可以存在于多个组中。
		/// 当改用户操作是，用他所拥有的最大Step。这样就可以跳过中间步骤了。
		/// </summary>
		/// <param name="currentUser"></param>
		/// <param name="optType"></param>
		/// <returns></returns>
		public static int GetUserMaxStepNumber(user currentUser, SupportTypes optType)
		{
			int?[] userAllProcessNumber = GetUserStepNumber(optType, currentUser);
			var max = userAllProcessNumber.Max();

			return (int)max;
		}

        /// <summary>
        /// Get the group data at the first step of the whole process
        /// Generally, this is cai gou yuan group
        /// </summary>
        /// <returns></returns>
        public static group GetFirstStepGroup()
        {
            return GlobalObjectBuffer.AllProcess.First().group; 
        }

        /// <summary>
        /// check if user belongs to the group
        /// </summary>
        /// <param name="user"></param>
        /// <param name="group"></param>
        /// <returns></returns>
        public static bool IsThisGroup(user user, group group)
        {
            var groups = GlobalObjectBuffer.GetGroupByUserId(user.idUser);
            return groups.Any(p => p.idGroup == group.idGroup);
        }

		/// <summary>
		/// 获得用户的操作权限
		/// </summary>
		/// <param name="currentUser"></param>
		/// <returns></returns>
		//public static List<BBAction> GetUserActionList(user currentUser)
		//{
		//    List<string> userActions = new List<string>();

		//    var usergroups = GlobalObjectBuffer.GetGroupByUserId(currentUser.idUser);
		//    foreach (var group in usergroups)
		//    {
		//        var actions = group.action.ToList().Select<action,string>(v => v.Name);
		//        userActions.AddRange(actions);
		//    }

		//    var rets = userActions.Select<string,BBAction>(v => (BBAction)Enum.Parse(typeof(BBAction),v,true));

		//    return rets.ToList();
		//}
	}
}